import { StoreMsg, InnerImgMsg, InnerCustomMsg, InnerTxtMsg, InnerAudioMsg } from "@/api/modules/common/IM";

export const buildStoreMsg = (msg: any): StoreMsg => {
  let msgObj: StoreMsg = {
    info: {
      from: msg.from,
      to: msg.to
    },
    chatType: msg.chatType as string,
    type: 2,
    time: msg.time
  };
  if (msg.type === "txt") {
    msgObj.msg = {
      data: [{ data: msg.msg, type: msg.type }],
      type: "txt",
      ext: msg.ext
    };
  }
  if (msg.type === "audio") {
  }
  if (msg.type === "img") {
    msgObj.msg = {
      ext: {},
      data: msg.url,
      type: "img"
    } as InnerImgMsg;
  }
  if (msg.type === "custom") {
    msgObj.msg = {
      ext: {
        jobId: msg.ext.jobId,
        type: msg.ext.type
      },
      type: "custom"
    } as InnerCustomMsg;
  }
  return msgObj;
};

export const parseStoreMsg = (msgObj: StoreMsg) => {
  let _msgObj: any = "";
  let info: { from: string; to: string } = JSON.parse(msgObj.info as string);
  let _type = JSON.parse(msgObj.msg as string).type;
  if (_type === "txt") {
    let _msg = JSON.parse(msgObj.msg as string) as InnerTxtMsg;
    _msgObj = {
      from: info.from,
      to: info.to,
      msg: _msg.data,
      type: "txt",
      time: msgObj.time as string
    };
  }
  if (_type === "audio") {
    let _msg = JSON.parse(msgObj.msg as string) as InnerAudioMsg;
    _msgObj = {
      from: info.from,
      to: info.to,
      time: msgObj.time as string,
      type: "audio",
      msg: "[语音]",
      ext: {
        url: _msg.data
      }
    };
  }
  if (_type === "img") {
    let _msg = JSON.parse(msgObj.msg as string) as InnerImgMsg;
    _msgObj = {
      from: info.from,
      to: info.to,
      url: _msg.data,
      thumb: _msg.data,
      type: "img",
      msg: "[图片]",
      time: msgObj.time as string
    };
  }
  if (_type === "custom") {
    let _msg = JSON.parse(msgObj.msg as string) as InnerCustomMsg;
    _msgObj = {
      from: info.from,
      to: info.to,
      ext: {
        jobId: _msg.ext.jobId,
        type: _msg.ext.type
      },
      msg: _msg.ext.type === "interview" ? "[面试邀请]" : "[岗位招聘]",
      type: "custom",
      time: msgObj.time as string
    };
  }
  return _msgObj;
};

export const parseLastMsg = (msg: string | Object) => {
  console.log("最后一条消息", msg);
  // let _msg:any
  // if(typeof msg === 'string'){
  //     _msg = JSON.parse(msg)
  // }else {
  //     _msg = (<any>msg).msg
  // }
  // let data = _msg.data as Array<{data:string,type:string}>
  // let text = ''
  // data.forEach(item => {
  //     if(item.type === 'txt'){
  //         text = text + item.data
  //     }
  //     if(item.type === 'emoji'){
  //         text = text + '[表情]'
  //     }
  // })
  // return text

  // 这个最后要删除
  return msg;
};
